package com.cjbdi.service;
import java.util.List;

import javax.transaction.Transactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import com.cjbdi.dao.SubscribeRepository;
import com.cjbdi.entity.Subscribe;
import com.cjbdi.pagination.PageInfo;
import com.cjbdi.vo.Result;

@Service
@Transactional
public class SubscribeService {
	@Autowired
    private SubscribeRepository subscribeRepository;

    public void save(Subscribe subscribe) {
    	subscribeRepository.save(subscribe);

    }


	public PageInfo<Subscribe> getPageByParam(String sfunction,Pageable pageable){
		Page<Subscribe> page = subscribeRepository.getPageByParam(sfunction, pageable);
		int records = (int) page.getTotalElements();
        int pageSize = page.getSize();
        int total = records % pageSize == 0 ? records / pageSize : records / pageSize + 1;
        PageInfo<Subscribe> pageInfo = new PageInfo<>();
        pageInfo.setPage(page.getNumber() + 1);//页码
        pageInfo.setPageSize(pageSize);//页面大小
        pageInfo.setRows(page.getContent());//分页结果
        pageInfo.setRecords(records);//总记录数
        pageInfo.setTotal(total);//总页数
		return pageInfo;
	}
	
	public List<Subscribe> findById(int id){
    	return subscribeRepository.findById(id);
	}
	
	public boolean delete(int id) {
		// TODO Auto-generated method stub
		Integer i = subscribeRepository.delete(id);
    	if(i>0) {
    		return true;
    	}else {
    		return false;
    	}
	}
	public List<Subscribe> findAll(){
		return subscribeRepository.findAll();
	}
	
}
